package com.wkbb.sports.websocket;

import lombok.Data;
import org.apache.commons.lang3.StringUtils;
import org.springframework.util.CollectionUtils;

import java.io.Serializable;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.CopyOnWriteArraySet;

/**
 * @author lbh
 * @date 2021年04月19日 20:58:33
 * @description: 组装webSocket服务器信息便于广播推送
 * 因为包含了javax.websocket.Session session属性的WebSocketServer对象无法被JSON.toJSONString()化，所以封装此类，将WebSocketServer类的6个业务参数独立出来，发送消息时将WebSocketServer转化为WebSocketInfoDto
 */
@Data
public class WebSocketInfoDto implements Serializable,Comparable<WebSocketInfoDto>{

        //当前连接学生的作业id
        private String workId;

        //当前连接学生的学生id
        private String studentId;

        //当前连接学生的实时跳绳数量【当前学生独享】
        private int number = 0;

        //当前连接学生的头像地址【当前学生独享】
        private String headImgUrl;

        //当前连接学生的名称【当前学生独享】
        private String studentName;

        //当前连接学生的实时时长【当前学生独享】
        private int duration;

        //该方法的返回值0代表相等，正数表示大于，负数表示小于
        //排序：优先按跳绳数量倒序排序
        @Override
        public int compareTo(WebSocketInfoDto o){
            int i =  o.getNumber()-this.getNumber() ;//优先按照跳绳数量倒序排序
            if(i == 0){
                return  this.getDuration()-o.getDuration();//如果跳绳数量相等了再用跳绳时间进行排序，时间越短越靠前
            }
            return i;
        }

        //合并查询出当前作业已经做的学生的成绩与【实时的学生成绩】再做总排序
        public static CopyOnWriteArrayList<WebSocketInfoDto> getWebSocketList(CopyOnWriteArraySet<WebSocketServer> webSocketSet,CopyOnWriteArrayList<WebSocketInfoDto> historyScoreStudentList){
            CopyOnWriteArrayList<WebSocketInfoDto> webSocketDtoList = new CopyOnWriteArrayList<WebSocketInfoDto>();//先将顺序的CopyOnWriteArraySet转化为有序的CopyOnWriteArrayList
            for (WebSocketServer item : webSocketSet) {
                if(StringUtils.isNotEmpty(item.getHeadImgUrl()) && StringUtils.isNotEmpty(item.getStudentName())  && item.getDuration()>0 && item.getNumber()>0){
                    WebSocketInfoDto webSocketInfoDto=new WebSocketInfoDto();
                    webSocketInfoDto.setWorkId(item.getWorkId());
                    webSocketInfoDto.setStudentId(item.getStudentId());
                    webSocketInfoDto.setNumber(item.getNumber());
                    webSocketInfoDto.setHeadImgUrl(item.getHeadImgUrl());
                    webSocketInfoDto.setStudentName(item.getStudentName());
                    webSocketInfoDto.setDuration(item.getDuration());
                    webSocketDtoList.add(webSocketInfoDto);
                }
            }

            if(!CollectionUtils.isEmpty(historyScoreStudentList)){
                webSocketDtoList.addAll(historyScoreStudentList);
            }

            Collections.sort(webSocketDtoList);//再对它进行降序排序
            return webSocketDtoList;
        }

        public static WebSocketInfoDto generateWebSocketInfoDto(WebSocketServer webSocketServer){
                WebSocketInfoDto webSocketInfoDto=new WebSocketInfoDto();
                webSocketInfoDto.setWorkId(webSocketServer.getWorkId());
                webSocketInfoDto.setStudentId(webSocketServer.getStudentId());
                webSocketInfoDto.setNumber(webSocketServer.getNumber());
                webSocketInfoDto.setHeadImgUrl(webSocketServer.getHeadImgUrl());
                webSocketInfoDto.setStudentName(webSocketServer.getStudentName());
                webSocketInfoDto.setDuration(webSocketServer.getDuration());
            return webSocketInfoDto;
        }

        //将com.wkbb.sports.dto.WebSocketInfoDto转化为com.wkbb.sports.websocket.WebSocketInfoDto
        public static CopyOnWriteArrayList<WebSocketInfoDto> transferWebSocketInfoDto(List<com.wkbb.sports.dto.WebSocketInfoDto> studentScoreOfWorkList){
            CopyOnWriteArrayList<WebSocketInfoDto> webSocketDtoList = new CopyOnWriteArrayList<WebSocketInfoDto>();
            studentScoreOfWorkList.forEach(studentScoreOfWork->{
                if(StringUtils.isNotEmpty(studentScoreOfWork.getHeadImgUrl()) && StringUtils.isNotEmpty(studentScoreOfWork.getStudentName()) && studentScoreOfWork.getDuration()>0 && studentScoreOfWork.getNumber()>0) {
                    WebSocketInfoDto webSocketInfoDto = new WebSocketInfoDto();
                    webSocketInfoDto.setWorkId(studentScoreOfWork.getWorkId());
                    webSocketInfoDto.setStudentId(studentScoreOfWork.getStudentId());
                    webSocketInfoDto.setNumber(studentScoreOfWork.getNumber());
                    webSocketInfoDto.setHeadImgUrl(studentScoreOfWork.getHeadImgUrl());
                    webSocketInfoDto.setStudentName(studentScoreOfWork.getStudentName());
                    webSocketInfoDto.setDuration(studentScoreOfWork.getDuration());
                    webSocketDtoList.add(webSocketInfoDto);
                }
            });

            return webSocketDtoList;
        }


}
